# -*- encoding: utf-8 -*-
from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

BET_ANALYSIS_CONFIG = {
    'real_time_report': False,
    'target_event_id': ['bet_order'],
    'interval_type_list': ['day', 'week', 'month'],
    'disable_global_aggregate': True,
    'group_by': 'bet_order_sub_type.keyword',
    'values': [
        {
            "value_id": "bet_user",
            "target_value": "_user_id",
            "value_name": "投注账号",
            "operation": "cardinality"
        },
        {
            "value_id": "_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "投注金额",
            "value_type": "float",
            "adjust_amount": True,
            "hide_result": True,
            "description": "游戏投注金额总和"
        },
        {
            "value_id": "bet_amount",
            "value_name": "投注金额",
            "value_expr": "_bet_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "游戏投注金额总和"
        },
        {
            "value_id": "bet_amount_avg",
            "value_name": "人均投注金额",
            "value_expr": "bet_amount/bet_user",
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount_avg",
            "value_name": "人均投注",
            "value_expr": "valid_bet_amount/bet_user",
            "adjust_amount": True,
        },
        {
            "value_id": "tax_amount",
            "target_value": "bet_order_tax_amount",
            "value_name": "税收金额",
            "value_type": "float",
            "adjust_amount": True
        },
        {
            "value_id": "profit_amount",
            "target_value": ['bet_order_bet_amount', '-', 'bet_order_award_amount'],
            "value_name": "玩法营收金额",
            "value_type": "float",
            "adjust_amount": True
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "adjust_amount": True,
        },
        {
            "value_id": "profit_margin",
            "value_name": "抽水率",
            "value_expr": "profit_amount/valid_bet_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "tax_margin",
            "value_name": "税收抽水占比",
            "value_expr": "tax_amount/profit_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
    ]
}
